package com.frxuexi.sign.view

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.databinding.ActivitySignBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

@Route(path = "/sign/SignActivity")
@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding, SignIntent, SignViewModel>() {
    override val binding: ActivitySignBinding by lazy { ActivitySignBinding.inflate(layoutInflater) }
    override val vm: SignViewModel by viewModels<SignViewModel>()

    var timer:Timer?=null
    override fun initData() {
    }

    override fun initView() {

        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val item = it.data as SignBeanItem
                        SPUtils.getInstance().put("id",item.id!!)
                        SPUtils.getInstance().put("status",item.status!!)
                        SPUtils.getInstance().put("dakaTime",item.morningCheckIn!!)

                        ToastUtils.showLong("打卡成功")
                    }

                    else -> {}
                }
            }
        }

        lifecycleScope.launch(Dispatchers.IO) {
            timer= Timer()
            timer?.schedule(timerTask {
                lifecycleScope.launch (Dispatchers.Main){
                    binding.signTime.setText(DateTimeUtils.getTime(System.currentTimeMillis()))
                }
            },0,1000)
        }

        val millis = System.currentTimeMillis()
        val date = DateTimeUtils.getDateFont(millis)
        val week = TimeUtils.getChineseWeek(millis).replace("周","星期")
        binding.signDate.setText("${date} ${week}")

        binding.conLocation.setOnClickListener {
            ArouterManager.nav(this,"/sign/MapActivity")
        }

        //sign:morning/after
       // date:2025-08-29
        //获取打卡日期
        val myDate = SPUtils.getInstance().getString("date","")
        if(myDate.isNotEmpty()){
            //打卡日期是不是今天
            if(myDate==DateTimeUtils.getDate(System.currentTimeMillis())){//今天打过卡
                val sign = SPUtils.getInstance().getString("sign", "")
                if(sign=="morning"){//说明打过上班卡
                    binding.signTvdaka.setText("下班打卡")
                    binding.signTvtime.setText("14:00-18:00")
                }else{//说明打过下班卡
                    binding.btnSign.isEnabled=false
                }
            }
        }



        binding.btnSign.setOnClickListener {
            val timeMillis = System.currentTimeMillis()
            val now = DateTimeUtils.getDateTime(timeMillis)
            var status="正常"
            if(binding.signTvdaka.text.toString()=="上班打卡"){
                SPUtils.getInstance().apply {
                    put("sign","morning")
                    put("date",DateTimeUtils.getDate(timeMillis))
                }
                binding.signTvdaka.setText("下班打卡")
                binding.signTvtime.setText("14:00-18:00")
                if(DateTimeUtils.getTime(timeMillis)>"09:00:00"){
                    status="迟到"
                }
                val item = SignBeanItem(
                    "", SPUtils.getInstance().getString("loc", ""), null, "天津市",
                    now, status, SPUtils.getInstance().getInt(Const.UID)
                )
                vm.sendIntent(SignIntent.addSign(item))
            }else{
                SPUtils.getInstance().apply {
                    put("sign","after")
                    put("date",DateTimeUtils.getDate(timeMillis))
                }
                binding.btnSign.isEnabled=false
                if(DateTimeUtils.getTime(timeMillis)<"18:00:00"){
                    status="早退"
                }
                val item = SignBeanItem(
                    now, SPUtils.getInstance().getString("loc", ""),
                    SPUtils.getInstance().getInt("id")
                    , "天津市",
                    SPUtils.getInstance().getString("dakaTime"),
                    SPUtils.getInstance().getString("status")+",$status"
                 , SPUtils.getInstance().getInt(Const.UID)
                )
                vm.sendIntent(SignIntent.addSign(item))
            }
        }

        binding.signList.setOnClickListener {
            ArouterManager.nav(this,"/sign/SignListActivity")
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer?.cancel()
            timer=null
        }
    }

}